import { createRouter, createWebHistory } from 'vue-router'
import SelectView from '../views/SelectView.vue'
import EditorView from '../views/EditorView.vue'
import Chapter5View from '../views/Chapter5View.vue'
import Chapter4ExamplesView from '../views/Chapter4ExamplesView.vue'
import Chapter3ExamplesView from '../views/Chapter3ExamplesView.vue'
import Chapter2View from '../views/Chapter2View.vue'
import Chapter1View from '../views/Chapter1View.vue'
import LectureView from '../views/LectureView.vue'
import AllLecturesView from '../views/AllLecturesView.vue'
import XmindView from '../views/XmindView.vue'
import LoginView from '../views/LoginView.vue'
import RegisterView from '../views/RegisterView.vue'
import KnowledgeBaseView from '../views/KnowledgeBaseView.vue'
import HomeView from '../views/HomeView.vue'
import ManagementView from '../views/ManagementView.vue'
import ChapterConfigView from '../views/ChapterConfigView.vue'
import ChapterHighlightsView from '../views/ChapterHighlightsView.vue'
import LectureMgr from '../views/LectureMgr.vue'
import LessonPlanGeneratorView from '../views/LessonPlanGeneratorView.vue'
import ModelConfigView from '../views/ModelConfigView.vue'
import SurveyView from '../views/SurveyView.vue'
import { useAuthStore } from '@/stores/auth'
import SampleView from '../views/SampleView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/home'
    },
    {
      path: '/home',
      name: 'home',
      component: HomeView,
      meta: { requiresAuth: true }
    },
    {
      path: '/select',
      name: 'select',
      component: SelectView,
      meta: { requiresAuth: true }
    },
    {
      path: '/editor',
      name: 'editor',
      component: EditorView,
      meta: { requiresAuth: true }
    },
    {
      path: '/sample',
      name: 'sample',
      component: SampleView,
      meta: { requiresAuth: true }
    },
    {
      path: '/examples',
      name: 'examples',
      component: Chapter5View,
      meta: { requiresAuth: true }
    },
    {
      path: '/chapter4-examples',
      name: 'chapter4-examples',
      component: Chapter4ExamplesView,
      meta: { requiresAuth: true }
    },
    {
      path: '/chapter3-examples',
      name: 'chapter3-examples',
      component: Chapter3ExamplesView,
      meta: { requiresAuth: true }
    },
    {
      path: '/chapter2',
      name: 'chapter2',
      component: Chapter2View,
      meta: { requiresAuth: true }
    },
    {
      path: '/chapter1',
      name: 'chapter1',
      component: Chapter1View,
      meta: { requiresAuth: true }
    },
    {
      path: '/lecture/:chapter',
      name: 'lecture',
      component: LectureView,
      meta: { requiresAuth: true }
    },
    {
      path: '/all-lectures',
      name: 'all-lectures',
      component: AllLecturesView,
      meta: { requiresAuth: true }
    },
    {
      path: '/xmind',
      name: 'xmind',
      component: XmindView,
      meta: { requiresAuth: true }
    },
    {
      path: '/login',
      name: 'login',
      component: LoginView,
      meta: { requiresGuest: true }
    },
    {
      path: '/register',
      name: 'register',
      component: RegisterView,
      meta: { requiresGuest: true }
    },
    {
      path: '/knowledge-base',
      name: 'knowledge-base',
      component: KnowledgeBaseView,
      meta: { requiresAuth: true }
    },
    {
      path: '/management',
      name: 'management',
      component: ManagementView,
      meta: { requiresAuth: true, requiresAdmin: true }
    },
    {
      path: '/chapter-config',
      name: 'chapter-config',
      component: ChapterConfigView,
      meta: { requiresAuth: true, requiresTeacher: true }
    },
    {
      path: '/chapter-highlights',
      name: 'chapter-highlights',
      component: ChapterHighlightsView,
      meta: { requiresAuth: true, requiresTeacher: true }
    },
    {
      path: '/lecture-mgr',
      name: 'lecture-mgr',
      component: LectureMgr,
      meta: { requiresAuth: true, requiresAdmin: true }
    },
    {
      path: '/lesson-plan-generator',
      name: 'lesson-plan-generator',
      component: LessonPlanGeneratorView,
      meta: { requiresAuth: true, requiresAdmin: true }
    },
    {
      path: '/model-config',
      name: 'model-config',
      component: ModelConfigView,
      meta: { requiresAuth: true, requiresAdmin: true }
    },
    {
      path: '/survey',
      name: 'survey',
      component: SurveyView,
      meta: { requiresAuth: true }
    },
    {
      path: '/chapter/:chapter',
      name: 'DynamicChapter',
      component: () => import('../views/DynamicChapterView.vue'),
      meta: { requiresAuth: true }
    }
  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const authStore = useAuthStore()
  
  // 恢复认证状态
  authStore.restoreAuth()
  
  // 检查是否需要认证
  if (to.meta.requiresAuth && !authStore.isAuthenticated) {
    next('/login')
    return
  }
  
  // 检查是否需要游客状态（已登录用户不能访问登录/注册页面）
  if (to.meta.requiresGuest && authStore.isAuthenticated) {
    next('/')
    return
  }
  
  // 检查是否需要管理员权限
  if (to.meta.requiresAdmin && !authStore.isAdmin) {
    next('/')
    return
  }
  
  // 检查是否需要教师权限（admin或teacher角色）
  if (to.meta.requiresTeacher && !['admin', 'teacher'].includes(authStore.user?.role || '')) {
    next('/')
    return
  }
  
  next()
})

export default router
